/* A simple standalone XML-RPC server written in C. 

    This basic debug server listens on port 8080 for xmlrpc data and process the mood.set method.
    
    Useful for frontend testing :)

*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <xmlrpc.h>
#include <xmlrpc_server.h>
#include <xmlrpc_abyss.h>

#define SLEEP(seconds) sleep(seconds);

static xmlrpc_value *
mood_set(xmlrpc_env *	const envP,
	 xmlrpc_value *	const paramArrayP,
	 void *		const serverInfo,
	 void *		const channelInfo) {
	 
    xmlrpc_int32 r, g, b, m;	/*red, green, blue and master values*/
    
    /* Parse our argument array. */
    xmlrpc_decompose_value(envP, paramArrayP, "(iiii)", &r, &g, &b, &m);
    if (envP->fault_occurred)
	return NULL;
    
    /*do some fancy shit!*/
    printf("Setting color: r=%d g=%d b=%d master=%d \n", r, g, b, m);
    
    /*return some feedback. */
    return xmlrpc_build_value(envP, "i", "ok");
}

int 
main(int           const argc, 
     const char ** const argv) {

    struct xmlrpc_method_info3 const methodInfo = {
        /* .methodName     =  "sample.add",*/
        /* .methodFunction =  &sample_add,*/
        /* .methodName     = */ "mood.set",
        /* .methodFunction = */ &mood_set,
    };
    xmlrpc_server_abyss_parms serverparm;
    xmlrpc_registry * registryP;
    xmlrpc_env env;


    xmlrpc_env_init(&env);

    registryP = xmlrpc_registry_new(&env);

    xmlrpc_registry_add_method3(&env, registryP, &methodInfo);

    /* In the modern form of the Abyss API, we supply parameters in memory
       like a normal API.  We select the modern form by setting
       config_file_name to NULL: 
    */
    serverparm.config_file_name = NULL;
    serverparm.registryP        = registryP;
    serverparm.port_number      = 8080;
    serverparm.log_file_name    = "/tmp/xmlrpc_log";

    printf("Running XML-RPC server...\n");

    xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(log_file_name));

    /* xmlrpc_server_abyss() never returns */

    return 0;
}
